/*输入验证*/
var login = new Validated();
login.rules = {
    "enddt": {
        "data": /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}(:\d{2})?)?$/,
        "complete": "ok",
        "error": "注意日期格式",
        "required": true
    },
    "price": {
        "data": /^[0-9]{1,}$/,
        "complete": "ok",
        "error": "只能为数字",
        "required": true
    },
    "taxpoint": {
        "data": /^[0-9.]{1,}$/,
        "complete": "ok",
        "error": "只能为数字",
        "required": true
    },
    "freight": {
        "data": /^[0-9]{1,}$/,
        "complete": "ok",
        "error": "只能为数字",
        "required": true
    },
}
login.blur().focus();
/*弹出报价框*/
var selectimgid, materielid;
var purchaseid = $(".details_p").attr("name");
$("#table tbody h1").click(function () {
    if (Cookie.Get("account") == "" || Cookie.Get("account") == null) {
        tanchuan("请登陆", "3", "/login/login", "1");
    } else {
        var sign = $(this).attr("value");
        if ($(".offer" + sign).css("display") == "none") {
            $(".background").show();
            materielid = $(this).attr("markers");
            selectimgid = $(this).parent().siblings().eq(0).text();
            var name = $(this).parent().siblings().eq(1).text();
            $(".offer_top h1").text("序号" + selectimgid);
            $(".offer_top h2").text("物料名称：" + name);
        } else {
            $(".submit_title").show();
            $(".materiel_details h3").show();
        }
    }
});
//关闭弹窗
var counts = "";
$(".offer h6").click(function () {
    if (login.complete("price", "enddt", "taxpoint", "freight")) {
        var inp = $(".offer").find("input");
        var enddt = inp.eq(0).val();
        var endd = new Date(enddt).getTime() / 1000;
        var price = inp.eq(1).val();
        var freight = inp.eq(3).val();
        var addr = inp.eq(4).val();
        var taxpoint = inp.eq(2).val();
        var spotgoods = $("input:radio:checked").val();
        var icourl = inp.eq(7).attr("value");
        $("#form_submit").append("<input type='hidden' name='enddt" + selectimgid + "' value='" + endd + "'>");
        $("#form_submit").append("<input type='hidden' name='price" + selectimgid + "' value='" + price + "'>");
        $("#form_submit").append("<input type='hidden' name='freight" + selectimgid + "' value='" + freight + "'>");
        $("#form_submit").append("<input type='hidden' name='addr" + selectimgid + "' value='" + addr + "'>");
        $("#form_submit").append("<input type='hidden' name='spotgoods" + selectimgid + "' value='" + spotgoods + "'>");
        $("#form_submit").append("<input type='hidden' name='taxpoint" + selectimgid + "' value='" + taxpoint + "'>");
        $("#form_submit").append("<input type='hidden' name='icourl" + selectimgid + "' value='" + icourl + "'>");
        $("#form_submit").append("<input type='hidden' name='materielid" + selectimgid + "' value='" + materielid + "'>");
        counts += selectimgid + ",";
        $(".background").hide();
        $(".offer" + selectimgid + " .td4 .part2 span").text(price);
        $(".offer" + selectimgid + " .td4 .part3 span").text(enddt);
        $(".offer" + selectimgid + " .td4 .part4 span").text(addr).attr("title", addr);
        $(".offer" + selectimgid + " .td4 .part5 span").text(freight);
        $(".offer" + selectimgid + " .td4 .part7 span").text(taxpoint);
        $(".offer" + selectimgid + " .td4 .part6 img").attr("src", "/upfile/" + icourl);
        $(".offer" + selectimgid + " .td5").val(selectimgid);
        $(".offer" + selectimgid + "").show();
        $(".swfuploadimg img").attr("src", "");
        inp.val("");
        $('[name="spotgoods"]').eq(0).prop("checked", true).val("1");
        $('[name="spotgoods"]').eq(1).prop("checked", false).val("0");
    }
})
/*提示语和提交判断*/
var lev = Cookie.Get("level");
var myid;
if (lev == "0") {
    myid = Cookie.Get("myid");
} else {
    myid = Cookie.Get("level");
}
$(".offer_top h3").click(function () {
    $(".background").hide();
})
$("#form_submit button").click(function () {
    if (myid == $("#caigouxm").attr("myid")) {
        tanchuan("不能为自己的项目报价", "3", "/purchase/list", "1");
    } else {
        if (!($("#form_submit").find("input").length - 1)) {
            $(".submit_title").show();
            $(".materiel_details h2").show();
        } else {
            if (Cookie.Get("account") == "" || Cookie.Get("account") == null) {
                tanchuan("请登陆", "3", "/login/login", "1");
            } else {
                new Ajax("/api/purchase/offer").keyvalue("myid", myid).keyvalue("counts", counts.substring(0, counts.length - 1)).keyvalue($("#form_submit").serializeArray()).getJson(function (data) {
                    if (data == 1) {
                        tanchuan("报价成功", "1", "/purchase/list", "1");
                    } else {
                        tanchuan("报价失败，请清理下缓存", "0", "", "0");
                    };
                })
            }
        }
    }
})
$("#form_submit button").mouseout(function () {
    $(".submit_title").fadeOut(2000);
    $(".materiel_details h2").fadeOut(2000);
});
/*删除*/
$(".td5").click(function () {
    $(this).parent().hide();
    var deleteid = $(this).val();
    $("#form_submit").find("[name='enddt" + deleteid + "']").remove();
    $("#form_submit").find("[name='price" + deleteid + "']").remove();
    $("#form_submit").find("[name='freight" + deleteid + "']").remove();
    $("#form_submit").find("[name='addr" + deleteid + "']").remove();
    $("#form_submit").find("[name='spotgoods" + deleteid + "']").remove();
    $("#form_submit").find("[name='icourl" + deleteid + "']").remove();
    $("#form_submit").find("[name='taxpoint" + deleteid + "']").remove();
    $("#form_submit").find("[name='materielid" + deleteid + "']").remove();
    counts = counts.replace(deleteid + ",", "");
});
$("#table tbody h1").mouseout(function () {
    $(".submit_title").fadeOut(2000);
    $(".materiel_details h3").fadeOut(2000);
})
/*大图显示*/